package selenium;


import TestSteps.TestSteps;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import pages.CabinetPage;
import pages.CartPage;
import pages.HomePage;
import pages.SearchPage;
import utils.ReportWriter;

public class FinalTestProject  extends TestSteps {


    @DataProvider(name = "userCredentialsReal")
    public Object[][] createSuccessTestData() {
        return new Object[][] {
                { "userName1XX@gamil.com","qwerty" }
        };
    }

    @DataProvider(name = "userCredentialsUnreal")
    public Object[][] createFailedTestData() {
        return new Object[][] {
                { "ewrfrfe@gamil.com","qwerty" },
                //{ "","" },
                //{ "fw","" },
                //{ "","dsvsfv" },
        };
    }


   @DataProvider(name = "searchData")
    public Object[][] createSuccessSearchData() {
        return new Object[][] {
                { "note" },
        };
    }

    @DataProvider(name = "searchSqlInjectionData")
    public Object[][] createSearchSqlInjectionData() {
        return new Object[][] {
                { "sumsung'" },
                { "note\"" },
        };
    }

    @DataProvider(name = "menuItemsIds")
    public Object[][] createSuccessIdsData() {
        return new Object[][] {
                { "computers-notebooks" },
                { "phones-mp3-gps" },
                { "tv-photo-video" },
                { "bt" },
                { "outdoorsman" },
                { "kids" },
                { "house_garden" },
                { "273290" },
                { "fatmenu_14" }
        };
    }

    @DataProvider(name = "SearchAndBuy")
    public Object[][] SearchAndBuy() {
        return new Object[][] {
                { "Modecom FreeTAB 1002 IPS", "modecom_tab-mc-tab-1002-ips-x4-bt/p354550/#cart", "Anna Ivanova", "Киев", "0959243766" ,"anna12@gmail.com"  },

        };
    }

    @DataProvider(name = "SearchAndBuyWrongData")
    public Object[][] SearchAndBuyWrongData() {
        return new Object[][] {

                { "Modecom FreeTAB 1002 IPS", "modecom_tab-mc-tab-1002-ips-x4-bt/p354550/#cart", "Katya Dan", "Киев", "095" ,"anna12@gmail.com"  },
        };
    }

    @DataProvider(name = "searchDataEmptyBasket")
    public Object[][] searchDataEmptyBasket() {
        return new Object[][] {
                { "Modecom FreeTAB 1002 IPS", "modecom_tab-mc-tab-1002-ips-x4-bt/p354550/#cart" }
        };
    }



    @Test(dataProvider = "searchData", priority = 6)
    public void SearchPositive(String searchText) {
        HomePage homepage = new HomePage(driver);
        String description = "Search test case";
        OpenPage(homepage);
        SearchPage searchpage = new SearchPage(driver);
        Search(homepage, searchpage, searchText);
        ReportWriter.logTestFinishSuccess(description);
    }

    // assume that if "search" return result as usual, sql injections are not possible
    @Test(dataProvider = "searchSqlInjectionData", priority = 7)
    public void SearchSqlInjectionTest(String searchText) {
        HomePage homepage = new HomePage(driver);
        String description = "Search Sql Injection test case";
        OpenPage(homepage);
        SearchPage searchpage = new SearchPage(driver);
        Search(homepage, searchpage, searchText);
        ReportWriter.logTestFinishSuccess(description);
    }

    @Test(dataProvider = "menuItemsIds", priority = 8)
    public void ClickMainMenuItem(String id) {
        String description = "Opening menu item "+id;
        HomePage homepage = new HomePage(driver);
        OpenPage(homepage);
        OpenMainMenuItem(homepage, id);
        ReportWriter.logTestFinishSuccess(description);
    }

    @Test(dataProvider = "SearchAndBuy", priority = 1)
    public void BuyGood(String good, String cartUrl, String name, String locality, String phone, String email) {
        String description = "Search And Buy";

        HomePage homepage = new HomePage(driver);
        SearchPage searchpage = new SearchPage(driver);
        CartPage cartpage = new CartPage(driver,cartUrl);
        CabinetPage cabinetpage = new CabinetPage(driver);

        OpenPage(homepage);
        Search(homepage, searchpage, good);
        BuyGood(searchpage,cartpage);
        FormOrder(cartpage,cabinetpage);
        FillFormForNewClient(cabinetpage, name, locality, phone, email);

        //empty basket
        OpenPage(homepage);
        EmptyBasketFromMainPage(homepage,cartpage);

        ReportWriter.logTestFinishSuccess(description);
    }

    @Test(dataProvider = "searchDataEmptyBasket", priority = 2)
    public void SearchAndBuyEmptyBasket(String good, String cartUrl) {
        String description = "Search And Buy (negative -> empty basket)";

        HomePage homepage = new HomePage(driver);
        SearchPage searchpage = new SearchPage(driver);
        CartPage cartpage = new CartPage(driver, cartUrl);

        OpenPage(homepage);
        Search(homepage, searchpage, good);
        BuyGood(searchpage,cartpage);
        EmptyBusket(cartpage);

        ReportWriter.logTestFinishSuccess(description);
    }

   @Test(dataProvider = "SearchAndBuyWrongData", priority = 3)
    public void SearchAndBuyWrongData(String good, String cartUrl, String name, String locality, String phone, String email) {
        String description = "Search And Buy (negative)";

        HomePage homepage = new HomePage(driver);
        SearchPage searchpage = new SearchPage(driver);
        CabinetPage cabinetpage = new CabinetPage(driver);
        CartPage cartpage = new CartPage(driver,cartUrl);

        OpenPage(homepage);
        Search(homepage, searchpage, good);
        BuyGood(searchpage,cartpage);
        FormOrder(cartpage,cabinetpage);
        FillFormForNewClientNegative(cabinetpage, name, locality, phone, email);

        //empty basket
        OpenPage(homepage);
        EmptyBasketFromMainPage(homepage,cartpage);

        ReportWriter.logTestFinishSuccess(description);
    }


    @Test(dataProvider = "userCredentialsReal", priority = 4)
    public void AutorizationPositive(String email, String password) {
        String description = "Autorization positive test case";
        HomePage homepage = new HomePage(driver);
        OpenPage(homepage);
        OpenAutorizationPopup(homepage);
        Signup(homepage, email,password,"positive");
        Logout(homepage);
        ReportWriter.logTestFinishSuccess(description);
    }

    @Test(dataProvider = "userCredentialsUnreal", priority = 5)
    public void AutorizationNegative(String email, String password) {
        String description = "Autorization negative test case";
        HomePage homepage = new HomePage(driver);
        OpenPage(homepage);
        OpenAutorizationPopup(homepage);
        Signup(homepage, email,password,"negative");
        ReportWriter.logTestFinishSuccess(description);
    }
}
